#include <core/riscv.h>

#include <QFile>
#include <QDebug>

/**
 * 创建一个RISCV内核对像
*/
riscv_t *riscv_create()
{
    // 创建RISC-V对像
    riscv_t * riscv = (riscv_t *)calloc(1, sizeof(riscv_t));
    assert(riscv!=NULL);
    return riscv;
}

void riscv_add_device(riscv_t *riscv, st_device *dev)
{
    dev->next = riscv->dev_list;
    riscv->dev_list = dev;
}

void riscv_set_flash(riscv_t *riscv, mem_t *dev)
{
    riscv->flash = dev;
}

void riscv_load_bin(riscv_t *riscv, QString file_name)
{
    QFile file(file_name);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug() << QString("open file %1 failed.\n").arg(file_name);
        exit(-1);
    }
    if(!file.size()){
        qDebug() << QString("file %1 is empty.\n").arg(file_name);
        exit(-1);
    }
    QByteArray tmpArr = file.readAll();
    std::memcpy(riscv->flash->mem, tmpArr.data(), tmpArr.size());
    file.close();
}

void riscv_reset(riscv_t *riscv)
{
    //仅重置所有寄存器即可
    riscv->pc = 0;
    riscv->instr.raw = 0;
    std::memset(riscv->regs, 0 ,RISCV_REGS_SIZE * sizeof(riscv_word_t));
}

st_device * riscv_find_device(riscv_t * riscv, riscv_word_t addr){
    st_device * curr = riscv->dev_list;
    while(curr){
        if((addr >= curr->base) && (addr < curr->end)){
            return curr;
        }
        curr = curr->next;
    }
    fprintf(stderr, "write to invalid mem %x.\n", addr);
    return nullptr;
}

int riscv_mem_read(riscv_t *riscv, riscv_word_t addr, uint8_t *val, int width)
{
    st_device *device = riscv_find_device(riscv, addr);
    if(device == nullptr){
        fprintf(stderr, "read to invalid memory: %x.\n", addr);
        return -1;
    }
    return device->read(device,addr,val,width);
}

int riscv_mem_write(riscv_t *riscv, riscv_word_t addr, uint8_t *val, int width)
{
    st_device *device = riscv_find_device(riscv, addr);
    if(device == nullptr){
        fprintf(stderr, "write to invalid memory: %x.\n", addr);
        return -1;
    }
    return device->write(device,addr,val,width);
}

void riscv_continue(riscv_t *riscv, int Isforever)
{
    //提取指令
    do{
        riscv_word_t pc = riscv->pc;
        riscv_word_t * mem = (riscv_word_t *)riscv->flash->mem;
        //
        if((pc >= riscv->flash->device.end) || (pc < riscv->flash->device.base)){
            return;
        }
        //
        /*
         * 因为riscv指令集是32位的，
         * 也就是说每一个指令都是32bit长度的！
         * 且riscv默认保持4字节对齐，
         * 因此除以4才能得到正确的下标位置
         */
        //
        riscv->instr.raw = ((riscv_word_t *)&mem[(pc - riscv->flash->device.base) >> 2])[0];

        switch (riscv->instr.opcode) {
        //
        case OP_BREAK:{
            return;
        }
        //
        case OP_LB:{
            switch(riscv->instr.I.funct3){
            case FUNC3_LB:{
                riscv_word_t val(0);
                riscv_word_t srcVal = riscv_read_reg(riscv, riscv->instr.I.rs1);
                riscv_word_t offset = i_get_imm(riscv->instr);
                riscv_word_t addr = srcVal + offset;
                riscv_mem_read(riscv, addr, (uint8_t *)&val, 1);
                riscv_write_reg(riscv, riscv->instr.I.rd, (val|(((val>>7)&0x1)?0xFFFFFF00:0)));
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_LH:{
                riscv_word_t val(0);
                riscv_word_t srcVal = riscv_read_reg(riscv, riscv->instr.I.rs1);
                riscv_word_t offset = i_get_imm(riscv->instr);
                riscv_word_t addr = srcVal + offset;
                riscv_mem_read(riscv, addr, (uint8_t *)&val, 2);
                riscv_write_reg(riscv, riscv->instr.I.rd, (val|(((val>>15)&0x1)?0xFFFF0000:0)));
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_LW:{
                riscv_word_t val(0);
                riscv_word_t srcVal = riscv_read_reg(riscv, riscv->instr.I.rs1);
                riscv_word_t offset = i_get_imm(riscv->instr);
                riscv_word_t addr = srcVal + offset;
                riscv_mem_read(riscv, addr, (uint8_t *)&val, 4);
                riscv_write_reg(riscv, riscv->instr.I.rd, val);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_LBU:{//LBU和LB的区别就是回写寄存器的时候是否需要符号扩展。
                riscv_word_t val(0);
                riscv_word_t srcVal = riscv_read_reg(riscv, riscv->instr.I.rs1);
                riscv_word_t offset = i_get_imm(riscv->instr);
                riscv_word_t addr = srcVal + offset;
                riscv_mem_read(riscv, addr, (uint8_t *)&val, 1);
                riscv_write_reg(riscv, riscv->instr.I.rd, val);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_LHU:{
                riscv_word_t val(0);
                riscv_word_t srcVal = riscv_read_reg(riscv, riscv->instr.I.rs1);
                riscv_word_t offset = i_get_imm(riscv->instr);
                riscv_word_t addr = srcVal + offset;
                riscv_mem_read(riscv, addr, (uint8_t *)&val, 2);
                riscv_write_reg(riscv, riscv->instr.I.rd, val);
                riscv->pc += sizeof(instr_t);
                break;
            }
            }
            break;
        }
        //
        case OP_SB:{
            //
            switch(riscv->instr.S.funct3){
            case FUNC3_SB:{
                riscv_word_t addr = riscv_read_reg(riscv, riscv->instr.S.rs1) + s_get_imm(riscv->instr);
                riscv_word_t val = riscv_read_reg(riscv, riscv->instr.S.rs2);
                riscv_mem_write(riscv, addr, (uint8_t *)&val, 1);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SH:{
                riscv_word_t addr = riscv_read_reg(riscv, riscv->instr.S.rs1) + s_get_imm(riscv->instr);
                riscv_word_t val = riscv_read_reg(riscv, riscv->instr.S.rs2);
                riscv_mem_write(riscv, addr, (uint8_t *)&val, 2);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SW:{
                riscv_word_t addr =riscv_read_reg(riscv, riscv->instr.S.rs1) + s_get_imm(riscv->instr);
                riscv_word_t val = riscv_read_reg(riscv, riscv->instr.S.rs2);
                riscv_mem_write(riscv, addr, (uint8_t *)&val, 4);
                riscv->pc += sizeof(instr_t);
                break;
            }
            default:
                break;
            }
            //
            break;
        }
        //
        case OP_ADDI:{
            switch(riscv->instr.I.funct3){
            case FUNC3_ADDI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int32_t immVal = (int32_t)i_get_imm(riscv->instr);
                riscv_write_reg(riscv, riscv->instr.I.rd, int32_t(regVal + immVal));
                // qDebug() << (int32_t)riscv_read_reg(riscv, riscv->instr.I.rd);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SLTI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int32_t immVal = (int32_t)i_get_imm(riscv->instr);
                int32_t ret = regVal < immVal ? 1 : 0;
                riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SLTIU:{
                uint32_t regVal = (uint32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                uint32_t immVal = (uint32_t)i_get_imm(riscv->instr);//符号扩展
                uint32_t ret = regVal < immVal ? 1 : 0;
                riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_XORI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int32_t immVal = (int32_t)i_get_imm(riscv->instr);
                int32_t ret = regVal ^ immVal;
                riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_ORI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int32_t immVal = (int32_t)i_get_imm(riscv->instr);
                int32_t ret = regVal | immVal;
                riscv_write_reg(riscv, riscv->instr.I.rd,ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_ANDI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int32_t immVal = (int32_t)i_get_imm(riscv->instr);
                int32_t ret = regVal & immVal;
                riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SLLI:{
                int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                int8_t immVal = (int8_t) (riscv->instr.I.imm11_0 & 0x3F); // 提取出移位的数量
                int32_t ret = (uint32_t)regVal << immVal;
                riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SR:{
                uint8_t key = (riscv->instr.I.imm11_0 >> 6) & 0x3F;
                if(key == 0){           /* SRLI */
                    int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                    int8_t immVal = (int8_t) (riscv->instr.I.imm11_0 & 0x3F); // 提取出移位的数量
                    int32_t ret = regVal >> immVal;
                    riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }else if(key == 0x10){  /* SRAI */
                    int32_t regVal = (int32_t)riscv_read_reg(riscv, riscv->instr.I.rs1);
                    int8_t immVal = (int8_t) (riscv->instr.I.imm11_0 & 0x3F); // 提取出移位的数量
                    int32_t ret = 0;
#if 0
                    if(regVal >= 0){
                        ret = regVal >> immVal;
                    }else{
                        ret = regVal >> immVal;
                        for(int i = 0; i < immVal; i++){
                            ret |= (0x80000000 >> i);
                        }
                    }
#else
                    ret = (int32_t)regVal >> immVal;
#endif
                    riscv_write_reg(riscv, riscv->instr.I.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }
                break;
            }
            default:{
                goto count_end;
            }
            }
            break;
        }
        //
        case OP_ADD:{
            switch(riscv->instr.R.funct3){
            case FUNC3_ADD:// (FUNC3_ADD / FUNC3_SUB)
            {
                int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                uint8_t key = (uint8_t)riscv->instr.R.funct7;
                int32_t ret = 0;
                switch(key){
                case FUNC7_ADD:{
                    ret = srcVal1 + srcVal2;
                    break;
                }
                case FUNC7_SUB:{
                    ret = srcVal1 - srcVal2;
                    break;
                }
                case FUNC7_MUL:{
                    ret = srcVal1 * srcVal2;
                    break;
                }
                }
                riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                riscv->pc += sizeof(instr_t);
                break;
            }
            case FUNC3_SLL:{
                switch(riscv->instr.R.funct7){
                //
                case FUNC7_ADD:{ // FUNC3_SLL
                    int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 << srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }break;
                //
                case FUNC7_MUL:{ // FUNC3_MULH
                    int64_t srcVal1 = s_extend_64(riscv_read_reg(riscv,riscv->instr.R.rs1));
                    int64_t srcVal2 = s_extend_64(riscv_read_reg(riscv,riscv->instr.R.rs2));
                    int32_t reth = ((srcVal1 * srcVal2) >> 32) & 0xFFFF'FFFF;
                    riscv_write_reg(riscv, riscv->instr.R.rd, reth);
                    riscv->pc += sizeof(instr_t);
                }break;
                //
                default:{
                    goto count_end;
                }
                }
                break;
            }
            case FUNC3_SLT:{
                //
                switch(riscv->instr.R.funct7){
                //
                case FUNC7_ADD:{ // FUNC3_SLT
                    int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 < srcVal2 ? 1 : 0;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                } break;
                //
                case FUNC7_MUL:{ // FUNC7_MULSU
                    int64_t srcVal1 = s_extend_64(riscv_read_reg(riscv,riscv->instr.R.rs1));
                    uint64_t srcVal2 = riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int64_t ret = ((srcVal1 * srcVal2) >> 32) & 0xFFFF'FFFF;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                } break;
                //
                default:
                    goto count_end;
                }
                break;
            }
            case FUNC3_SLTU:{
                //
                switch(riscv->instr.R.funct7){
                //
                case FUNC7_ADD:{ // FUNC3_SLTU
                    uint32_t srcVal1 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    uint32_t srcVal2 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 < srcVal2 ? 1 : 0;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                } break;
                //
                case FUNC7_MUL:{ // FUNC7_MULSU
                    uint64_t srcVal1 = riscv_read_reg(riscv,riscv->instr.R.rs1);
                    uint64_t srcVal2 = riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int64_t ret = ((srcVal1 * srcVal2) >> 32) & 0xFFFF'FFFF;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                } break;
                //
                default:
                    goto count_end;
                    //
                }
                break;
            }
            case FUNC3_XOR:{
                //
                switch(riscv->instr.R.funct7){
                case FUNC7_ADD:{
                    int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 ^ srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }break;
                case FUNC7_DIV:{ // FUNC7_DIV
                    int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 / srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }break;
                default:
                    goto count_end;
                }
                break;
            }
            case FUNC3_SRL:{//FUNC3_SRL | FUNC3_SRA
                switch(riscv->instr.R.funct7){
                case FUNC7_SRL:{//逻辑右移
                    uint32_t srcVal1 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    uint32_t srcVal2 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 >> srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                    break;
                }
                case FUNC7_SRA:{//算术右移
                    int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    int32_t ret = srcVal1 >> srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                    break;
                }
                case FUNC7_DIV:{// FUNC7_DIVU
                    uint32_t srcVal1 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                    uint32_t srcVal2 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                    uint32_t ret = srcVal1 / srcVal2;
                    riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                    riscv->pc += sizeof(instr_t);
                }break;
                default:
                    goto count_end;
                }
            }
            break;
            }
        case FUNC3_OR:{
            switch(riscv->instr.R.funct7){
            case FUNC7_ADD:{
                int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                int32_t ret = srcVal1 | srcVal2;
                riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                riscv->pc += sizeof(instr_t);
            } break;
            case FUNC7_REM:{
                int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                int32_t ret = srcVal1 % srcVal2;
                riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                riscv->pc += sizeof(instr_t);
            } break;
            default:
                goto count_end;
            }
        }
        break;
        }
        case FUNC3_AND:{
            switch(riscv->instr.R.funct7){
            case FUNC7_ADD:{
                int32_t srcVal1 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                int32_t srcVal2 = (int32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                int32_t ret = srcVal1 & srcVal2;
                riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                riscv->pc += sizeof(instr_t);
            } break;
            case FUNC7_REM:{
                uint32_t srcVal1 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs1);
                uint32_t srcVal2 = (uint32_t)riscv_read_reg(riscv,riscv->instr.R.rs2);
                uint32_t ret = srcVal1 % srcVal2;
                riscv_write_reg(riscv, riscv->instr.R.rd, ret);
                riscv->pc += sizeof(instr_t);
            } break;
            default:{
                goto count_end;
            }
            }
            break;
        }
        //
        case OP_LUI:{
            quint32 ret = (quint32)riscv->instr.U.imm31_12 << 12;
            riscv_write_reg(riscv, riscv->instr.U.rd, ret);
            riscv->pc += sizeof(instr_t);
            break;
        }
        //
        case OP_AUIPC:{
            quint32 ret = (quint32)riscv->instr.U.imm31_12 << 12;
            riscv_write_reg(riscv, riscv->instr.U.rd, (ret + riscv->pc));
            riscv->pc += sizeof(instr_t);
            break;
        }
        //
        case OP_JAL:{
            int32_t imm = j_get_imm(riscv->instr);
            riscv_write_reg(riscv, riscv->instr.J.rd, (riscv->pc + 4));
            riscv->pc = riscv_word_t(riscv->pc + imm);
            break;
        }
        //
        case OP_JALR:{
            riscv_write_reg(riscv, riscv->instr.I.rd, (riscv->pc + 4));
            riscv->pc = riscv_word_t(riscv_read_reg(riscv, riscv->instr.I.rs1) + uint32_t(i_get_imm(riscv->instr)));
            break;
        }
        //
        case OP_BEQ:{
            switch(riscv->instr.B.funct3){
            case FUNC3_BEQ:{
                riscv_word_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                riscv_word_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 == reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            case FUNC3_BNE:{
                riscv_word_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                riscv_word_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 != reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            case FUNC3_BLT:{
                int32_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                int32_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 < reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            case FUNC3_BGE:{
                int32_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                int32_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 >= reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            case FUNC3_BLTU:{
                riscv_word_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                riscv_word_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 < reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            case FUNC3_BGEU:{
                riscv_word_t reg1 = riscv_read_reg(riscv, riscv->instr.B.rs1);
                riscv_word_t reg2 = riscv_read_reg(riscv, riscv->instr.B.rs2);
                if(reg1 >= reg2){
                    int32_t imm = b_get_imm(riscv->instr);
                    riscv->pc = riscv_word_t(riscv->pc + imm);
                }else{
                    riscv->pc += sizeof(instr_t);//跳转到下一条指令去执行
                }
            }break;
            default:break;
            }
        }break;
        //
        default:{
            riscv->pc += sizeof(riscv_word_t);
        }break;
            //
        }
    }while(Isforever);

    return;

count_end:
    fprintf(stderr, "unknown instr 0x%x\n", riscv->instr.raw);
}
